package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import dao.Connector;
import constants.*;
import java.util.logging.Level;
import java.util.logging.Logger;
// Class thêm, sửa xóa việc đặt phòng
public class Booking {
	Connection connect =null;
	PreparedStatement pstm=null;
	private String table="tblBooking";	
	public boolean addBooking(int id_room, int id_customer){
		//Hàm thêm booking mới
		connect = Connector.getConnect();
		try{
			pstm= connect.prepareStatement("INSERT INTO "+this.table+"(id_room,id_customer) VALUES (?,?)");
			pstm.setInt(1,id_room);
			pstm.setInt(2,id_customer);
			if(pstm.executeUpdate()>0){
				return true;
			}
	
		}catch(SQLException e){
			e.printStackTrace();
		}
		return false;
		
	}
	public boolean deleteBooking(int id){
		connect = Connector.getConnect();
		try{
			pstm = connect.prepareStatement("DELETE FROM "+this.table+ " WHERE id_customer = ?");
			pstm.setInt(1, id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return false;
	}
	public boolean updateBooking(int id,int id_room, int id_customer){
		connect= Connector.getConnect();
		try{
			pstm = connect.prepareStatement("UPDATE "+this.table +" SET id_room=?,id_customer=? WHERE id=?");
			pstm.setInt(1,id_room );
			pstm.setInt(2,id_customer);
			pstm.setInt(6,id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public ArrayList<entity.Booking> getAllBooking(){
		connect= Connector.getConnect();
		ResultSet rs=null;
		ArrayList<entity.Booking> list = new ArrayList<>();
		try{

			pstm= connect.prepareStatement("Select *from "+this.table);
			rs=pstm.executeQuery();
                        Room room ;
                        Customer customer;
			while(rs.next()){                            
			entity.Booking booking = new entity.Booking();
                        room = new Room();
                        customer = new Customer();
			booking.setId_booking(rs.getInt("id"));
			booking.setId_customer(rs.getInt("id_customer"));
			booking.setId_room(rs.getInt("id_room"));
                        booking.setRoom(room.getRoomById(booking.getId_room()));
                        booking.setCustomer(customer.getCustomerById(booking.getId_customer()));       
			list.add(booking);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return list;
	}
	public entity.Booking getBookingById(int id){
		
		connect = Connector.getConnect();
		ResultSet rs=null;
                entity.Booking booking =null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where id=?");
			pstm.setInt(1, id);
			rs= pstm.executeQuery();
			if(rs.next()){
                                booking=new entity.Booking();
				booking.setId_customer(rs.getInt("id_customer"));
				booking.setId_room(rs.getInt("id_room"));
			}
	
			// Lay thong tin gui den Entity
		}catch(SQLException e){
			e.printStackTrace();
		}
                return booking;
	}       
        public ArrayList<entity.Room> getRoombyIdCustomer(int id){
                    connect= Connector.getConnect();
                    ResultSet rs;
                    ArrayList<entity.Room> list = new ArrayList<>();
                    try{
                            pstm= connect.prepareStatement("Select *from "+this.table+" where id_customer=?");
                            pstm.setInt(1, id);
                            rs=pstm.executeQuery();
                            Room room ;
                            while(rs.next()){
                                room = new Room();
                                entity.Booking booking = new entity.Booking();                            
                                booking.setRoom(room.getRoomById(rs.getInt("id_room")));
                                list.add(booking.getRoom()); 
                            }
                    }catch(SQLException e){
                            e.printStackTrace();
                    }
                    return list;
        }       
        public ArrayList<entity.Booking> getAllById(int id){

                    connect= Connector.getConnect();
                    ResultSet rs;
                    ArrayList<entity.Booking> list = new ArrayList<>();
                    try{

                            pstm= connect.prepareStatement("Select *from "+this.table+" where id_customer=?");
                            pstm.setInt(1, id);
                            rs=pstm.executeQuery();
                            Room room ;
                            Customer customer;
                            if(rs.next()){
                                entity.Booking booking = new entity.Booking();
                                room = new Room();
                                customer = new Customer();
                                booking.setId_booking(rs.getInt("id"));
                                booking.setId_customer(rs.getInt("id_customer"));
                                booking.setId_room(rs.getInt("id_room"));
                                booking.setRoom(room.getRoomById(booking.getId_room()));
                                booking.setCustomer(customer.getCustomerById(id));
                                list.add(booking);
                            }
                    }catch(SQLException e){
                            e.printStackTrace();
                    }
                    return list;
        }
        public entity.Customer getCustomerByRoom(int id){
           
		connect= Connector.getConnect();
		ResultSet rs;
                 Customer customer;
                 entity.Booking booking = null ;
		try{
                        
			pstm= connect.prepareStatement("Select *from "+this.table+" where id_room=?");
                        pstm.setInt(1, id);
			rs=pstm.executeQuery();
                       
                        while(rs.next()){
                            booking= new entity.Booking();
                            customer = new Customer();
                            booking.setCustomer(customer.getCustomerById(rs.getInt("id_customer")));
                            if(booking.getCustomer().getInfo().getStatus()!=Constants.STATUS_CHECKOUT
                                    &&booking.getCustomer().getInfo().getStatus()!=Constants.STATUS_FULL){
                                return booking.getCustomer();
                            }
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return booking.getCustomer();
       }        	
}
